package com.stellar.circle.domain.sPostCategory.impl;

import com.stellar.circle.basic.entity.SPostCategory;
import com.stellar.circle.basic.service.SPostCategoryService;
import com.stellar.circle.controller.admin.sPostCategory.vo.SPostCategoryRequestPageVO;
import com.stellar.circle.controller.admin.sPostCategory.vo.SPostCategoryRequestVO;
import com.stellar.circle.controller.admin.sPostCategory.vo.SPostCategoryResponseVO;
import com.stellar.circle.convert.SPostCategoryConvert;
import com.stellar.circle.domain.sPostCategory.SPostCategoryDomainService;

import com.stellar.web.core.bean.PageResult;
import com.stellar.web.core.enums.IsDeletedFlagEnum;
import com.stellar.web.core.enums.IsEnabledFlagEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * 帖子分类表领域服务实现
 *
 * @author Jqchan
 * @version 1.0
 * @date 2025-05-17 19:12:03
 */
@Service
public class SPostCategoryDomainServiceImpl implements SPostCategoryDomainService {

    @Resource
    private SPostCategoryService sPostCategoryService;

    @Override
    public PageResult<SPostCategoryResponseVO> page(SPostCategoryRequestPageVO requestPageVO) {
        PageResult<SPostCategoryResponseVO> pageResult = new PageResult<>();
        SPostCategory queryEntity = SPostCategoryConvert.INSTANCE.convert(requestPageVO);
        queryEntity.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getVal());

        List<SPostCategory> entities = sPostCategoryService.page(queryEntity,
                requestPageVO.getStart(), requestPageVO.getPageSize());

        if (entities.isEmpty()) {
            return pageResult;
        }

        Long count = sPostCategoryService.count(queryEntity);
        List<SPostCategoryResponseVO> vos = SPostCategoryConvert.INSTANCE.convertEntityToRespList(entities);

        pageResult.setResult(vos);
        pageResult.setTotal(count.intValue());
        return pageResult;
    }

    @Override
    public Boolean update(SPostCategoryRequestVO requestVO) {
        SPostCategory entity = SPostCategoryConvert.INSTANCE.convert(requestVO);
        return sPostCategoryService.update(entity) != 0;
    }

    @Override
    public Boolean delete(Long id) {
        SPostCategory entity = new SPostCategory();
        entity.setId(id);
        entity.setIsDeleted(IsDeletedFlagEnum.DELETED.getVal());
        return sPostCategoryService.update(entity) != 0;
    }

    @Override
    public Boolean insert(SPostCategoryRequestVO requestVO) {
        SPostCategory entity = SPostCategoryConvert.INSTANCE.convert(requestVO);
        entity.setIsEnabled(IsEnabledFlagEnum.ENABLED.getVal());
        return sPostCategoryService.insert(entity) != 0;
    }

    @Override
    public SPostCategoryResponseVO get(Long id) {
        SPostCategory entity = sPostCategoryService.queryById(id);
        return SPostCategoryConvert.INSTANCE.convert(entity);
    }

    @Override
    public List<SPostCategoryResponseVO> list() {
        SPostCategory sPostCategory = new SPostCategory();
        sPostCategory.setIsEnabled(IsEnabledFlagEnum.ENABLED.getVal());
        sPostCategory.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getVal());
        List<SPostCategory> sPostCategories = sPostCategoryService.queryAllByCondition(sPostCategory);
        return SPostCategoryConvert.INSTANCE.convertEntityToRespList(sPostCategories);
    }
}
